package com.zeei.ems.meet.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zeei.ems.config.StateConfig;
import com.zeei.ems.meet.dao.DataRtDao;
import com.zeei.ems.meet.dao.EquipDao;
import com.zeei.ems.meet.dao.StatLatestDao;
import com.zeei.ems.meet.model.DataRtVO;
import com.zeei.ems.meet.model.EquipVO;
import com.zeei.ems.meet.model.StatLatestVO;
import com.zeei.ems.meet.model.vo.DataRtParam;
import com.zeei.ems.meet.model.vo.EquipParam;
import com.zeei.ems.meet.model.vo.StatLatestParam;
import com.zeei.ems.meet.service.EquipService;
import com.zeei.ems.meet.util.BeanMapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EquipServiceImpl implements EquipService {

    @Autowired
    EquipDao equipDao;
    @Autowired
    StatLatestDao statLatestDao;
    @Autowired
    DataRtDao dataRtDao;
    @Autowired
    StateConfig stateConfig;

    @Override
    public List<EquipVO> listEquip(EquipParam equip) {
        return equipDao.listEquip(equip);
    }

    @Override
    public List<EquipVO> listEquipData(EquipParam equip) {
        List<EquipVO> resultList = new ArrayList();
        // 查询设备数据
        List<EquipVO> equipList = equipDao.listEquipAndPollute(equip);
        if (!CollectionUtil.isEmpty(equipList)){
            Map<String, List<EquipVO>> mapList = equipList.stream().collect(Collectors.groupingBy(EquipVO::getEquipCode));
            List<String> equipCodeList = new ArrayList<>();
            for (Map.Entry<String, List<EquipVO>> entry : mapList.entrySet()) {
                equipCodeList.add(entry.getKey());
                resultList.add(entry.getValue().get(0));
            }

            // 查询状态数据
            StatLatestParam statLatestParam = BeanMapperUtils.map(equip, StatLatestParam.class);
            statLatestParam.setEquipCodeList(equipCodeList);
            statLatestParam.setStateTime(stateConfig.getStateTime());
            List<StatLatestVO> statLatestList = statLatestDao.listStatLatest(statLatestParam);

            // 查询监测数据
            List<String> polluteCodeList = new ArrayList<>();
            equipList.forEach(item ->{
                if (!item.getPolluteCode().equals(equip.getStatusCode())) {
                    polluteCodeList.add(item.getPolluteCode());
                }
            });
            DataRtParam dataRtParam = new DataRtParam();
            dataRtParam.setPointCode(equip.getPointCode());
            //dataRtParam.setEquipCodeList(equipCodeList);
            dataRtParam.setPolluteCodeList(polluteCodeList);
            List<DataRtVO> dataRtList = dataRtDao.listDataRtTopOne(dataRtParam);

            // 绑定数据
            resultList.forEach(item -> {
                if (!CollectionUtil.isEmpty(statLatestList)){
                    List<StatLatestVO> collectList = statLatestList.stream().filter(statLatest ->
                            statLatest.getEquipCode().equals(item.getEquipCode())).collect(Collectors.toList());
                    if (!CollectionUtil.isEmpty(collectList)){
                        item.setStatLatest(collectList.get(0));
                    }
                }
                // 当前为获取到状态数据，默认设置为离线
                if (item.getStatLatest() == null){
                    StatLatestVO statLatestVO = new StatLatestVO();
                    statLatestVO.setPointCode(item.getPointCode());
                    statLatestVO.setDataValue("12");
                    item.setStatLatest(statLatestVO);
                }

                if (!CollectionUtil.isEmpty(dataRtList)) {
                    List<EquipVO> equips = mapList.get(item.getEquipCode());
                    polluteCodeList.clear();
                    equips.forEach(data -> {
                        if (!data.getPolluteCode().equals(equip.getStatusCode())) {
                            polluteCodeList.add(data.getPolluteCode());
                        }
                    });

                    List<DataRtVO> dataList = dataRtList.stream().filter(data ->
                            polluteCodeList.contains(data.getPolluteCode()) ).collect(Collectors.toList());
                    if (!CollectionUtil.isEmpty(dataList)) {
                        item.setDataRtList(dataList);
                    }
                }
            });
        }

        return resultList;
    }
}
